<?php
/**
 * @author Ngo Minh Tri <minhtri2711@gmail.com>
 */
require_once APPPATH."controllers/cmsbase.php";
class cms_user extends CmsBase {

	var $_require_login = true;
	var $controller_js_path = 'cms_user/main';

	function __construct(){
		parent::__construct();
		// Check permission
		parent::_check_permission('pms_suser');
		$this->load->model('Model_user', 'model_user');
		$this->load->model('Model_role', 'model_role');
	}

	/**
	 *
	 *
	 */
	function index(){
		return $this->_render_my_view(array());
	}
	/**
	 *
	 *
	 */
	function browse_user(){
		$this->load->library('pagination');
		$users = $this->model_user->fetch_all();

		$start = 0;
		$limit = 10;
		$total = count($users);
		$per_page = isset($_GET['per_page']) ? intval($_GET['per_page']):0;
		if($per_page){
			$start = $per_page;
		}
		$config['base_url'] = "?c=cms_user&m=browse_user";
		$config['uri_segment'] = 3;
		$config['total_rows'] = $total;
		$config['per_page'] = $limit;
		$this->pagination->initialize($config);

		return $this->load->view("cms_user/browse_user", array('users'=>$users));
	}
	/**
	 *
	 *
	 */
	function add_user(){
		if($_SERVER['REQUEST_METHOD'] === 'POST'){
			// Check is method POST
			$this->load->library('form_validation');
			$this->form_validation->set_rules('item_loginname', 'Login Name', 'trim|required|callback_userloginame_check');
			$this->form_validation->set_rules('item_email', 'Email', 'trim|required|valid_email|callback_useremail_check');
			if($this->form_validation->run() === FALSE){
				$user_data['status'] = 'failure';
				$user_data['messages'] = $this->form_validation->error_string();
				echo json_encode($user_data);
			}
			else{
				// validation
				$data = parse_input("item_", $_POST);
				// Check change passwd
				if(!empty($data['passwd'])){
					$data['passwd'] = md5($data['passwd']);
				}
				unset($data['passwd_conf']);
				$user_id = $this->model_user->insert_user($data);
				//redirect('cms_user/index', 'refresh');
				$user_data = $this->model_user->find($user_id);
				$user_data['status'] = 'success';
				echo json_encode($user_data);
			}
			exit(0);
		}
		$roles = $this->model_role->fetch_all();
		return $this->load->view("cms_user/add_user", array("roles" => $roles));
	}
	/**
	 *
	 *
	 */
	function edit_user($user_id){
		if(intval($user_id)){
			if($_SERVER['REQUEST_METHOD'] === 'POST'){
				// validation
				$data = parse_input("item_", $_POST);
				// Check change passwd
				if(!empty($data['passwd'])){
					$data['passwd'] = md5($data['passwd']);
				}
				unset($data['id']);
				unset($data['passwd']);
				unset($data['passwd_conf']);
				$this->model_user->update_user($user_id, $data);
				$user_data = $this->model_user->find($user_id);
				$user_data['status'] = 'success';
				echo json_encode($user_data);
				exit(0);
			}
			$roles = $this->model_role->fetch_all();
			$user_data = $this->model_user->find($user_id);
			return $this->load->view("cms_user/edit_user", array('user' => $user_data, "roles" => $roles));
		}
	}

	/**
	 *
	 *
	 */
	function find_user($user_id){
		$user_data = $this->model_user->find($user_id);
		return $this->load->view("cms_user/find_user", array('item' => $user_data));
	}

	/**
	 *
	 *
	 */
	function delete_user($user_id){
		$this->model_user->delete_user($user_id);
		exit(0);
	}

	/**
	 *
	 *
	 */
	function userloginame_check($str){
		$id = $this->input->post("item_id");
		// Load email
		$isCheck = $this->model_user->check_loginname($str, $id);
		if (is_array($isCheck))
		{
			// Email already exists! Please input another email!
			$message = "Login name already exists! Please input another login name!";
			$this->form_validation->set_message('userloginame_check', $message);
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}
	/**
	 *
	 *
	 */
	function useremail_check($str){
		$id = $this->input->post("item_id");
		// Load email
		$isCheck = $this->model_user->check_email($str, $id);
		if (is_array($isCheck))
		{
			// Email already exists! Please input another email!
			$message = "Email already exists! Please input another email!";
			$this->form_validation->set_message('useremail_check', $message);
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}

	/**
	 *
	 *
	 */
	function browse_role(){
		$roles = $this->model_role->fetch_all();
		//var_dump($roles);
		return $this->load->view("cms_user/browse_role", array('roles'=>$roles));
	}

	/**
	 *
	 *
	 */
	function add_role(){
		if($_SERVER['REQUEST_METHOD'] === 'POST')
		{
			// validation
			$data = parse_input("item_", $_POST);
			$role_id = $this->model_role->insert_role($data);
			$role_data = $this->model_role->find($role_id);
			$role_data['status'] = 'success';
			echo json_encode($role_data);
			exit(0);
		}
		else{
			return $this->load->view("cms_user/add_role");
		}
	}

	/**
	 *
	 *
	 */
	function edit_role($role_id){
		if(intval($role_id)){
			if($_SERVER['REQUEST_METHOD'] === 'POST'){
				// validation
				$data = parse_input("item_", $_POST);
				if(isset($data['id'])) unset($data['id']);

				$this->model_role->update_role($role_id, $data);
				$role_data = $this->model_role->find($role_id);
				$role_data['status'] = 'success';
				echo json_encode($role_data);
				exit(0);
			}
			else{
				$role_data = $this->model_role->find($role_id);
				return $this->load->view("cms_user/edit_role", array('role' => $role_data));
			}
		}
	}

	/**
	 *
	 *
	 */
	function delete_role($role_id){
		$this->model_role->delete_role($role_id);
		exit(0);
	}
	/**
	 *
	 *
	 */
	function find_role($role_id){
		$role_data = $this->model_role->find($role_id);
		return $this->load->view("cms_user/find_role", array('item' => $role_data));
	}
}
?>
